package com.traffic.fuwu.util;




import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;

/**
 * 工具类
 * 作者 ：皮可为
 * 邮箱 ：329736233@qq.com
 * 日期 ：2015/7/29
 */
public class WebTool {

    /**
     * 获取项目基URL
     * <br>例子:http://www.baidu.com/proj
     * <br>例子:http://www.baidu.com:8080/proj
     * @param req
     * @return url地址
     */
    public static String getBasePath(HttpServletRequest req){
        String path = req.getContextPath();
        int port = req.getServerPort();
        if(port == 80){
            return req.getScheme()+"://"+req.getServerName()+path;
        }
        return req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path;
    }

    /**
     * 获取classPath下资源文件
     * @param path
     * @return
     */
    public static File getResourceFile(String path){
        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:" + path);
        } catch (FileNotFoundException e) {
            return null;
        }
        return file;
    }
    
    /**
     * 将数字字符串转Decimal
     * @param number
     * @return
     */
    public static BigDecimal getDecimal(String number){
        BigDecimal decimal = NumberUtils.createBigDecimal(number);
        decimal.setScale(2,BigDecimal.ROUND_HALF_UP);
        return decimal;
    }

    public static void main(String[] args) {
		System.out.println(getDecimal("2.00"));
	}
}
